﻿using GalaSoft.MvvmLight;
using System.Windows.Input;
using GalaSoft.MvvmLight.Command;
using ASoft.Service;

namespace ASoft.WPF.ViewModel
{
    /// <summary>
    /// This class contains properties that a View can data bind to.
    /// <para>
    /// Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel.
    /// </para>
    /// <para>
    /// You can also use Blend to data bind with the tool's support.
    /// </para>
    /// <para>
    /// See http://www.galasoft.ch/mvvm/getstarted
    /// </para>
    /// </summary>
    public class SerialPortViewModel : ViewModelBase
    {
        /// <summary>
        /// Initializes a new instance of the SerialPortViewModel class.
        /// </summary>
        public SerialPortViewModel()
        {
            if (IsInDesignMode)
            {
                this._PortName = "COM1";
                this.PortNameSource = new string[] { "COM1", "COM2" };
            }
            else
            {
                this._PortName = "COM3";
                this.PortNameSource = SerialPortService.GetPortNames();
            }
        }

        ////public override void Cleanup()
        ////{
        ////    // Clean own resources if needed

        ////    base.Cleanup();
        ////}


        /// <summary>
        /// The <see cref="PortName" /> property's name.
        /// </summary>
        public const string PortNamePropertyName = "PortName";

        private string _PortName = string.Empty;

        /// <summary>
        /// Gets the PortName property.
        /// </summary>
        public string PortName
        {
            get
            {
                return _PortName;
            }

            set
            {
                if (_PortName == value)
                {
                    return;
                }

                var oldValue = _PortName;
                _PortName = value;

                // Update bindings, no broadcast
                RaisePropertyChanged(PortNamePropertyName);

                // Update bindings and broadcast change using GalaSoft.MvvmLight.Messenging
                //RaisePropertyChanged(PortNamePropertyName, oldValue, value, true);
            }
        }

        public string[] PortNameSource { get; private set; }


        /// <summary>
        /// The <see cref="Status" /> property's name.
        /// </summary>
        public const string StatusPropertyName = "Status";

        private bool _Status = false;

        /// <summary>
        /// Gets the Status property.
        /// TODO Update documentation:
        /// Changes to that property's value raise the PropertyChanged event. 
        /// This property's value is broadcasted by the Messenger's default instance when it changes.
        /// </summary>
        public bool Status
        {
            get
            {
                return _Status;
            }

            set
            {
                if (_Status == value)
                {
                    return;
                }

                var oldValue = _Status;
                _Status = value;

                // Update bindings, no broadcast
                RaisePropertyChanged(StatusPropertyName);

            }
        }

        /// <summary>
        /// The <see cref="StatusText" /> property's name.
        /// </summary>
        public const string StatusTextPropertyName = "StatusText";

        private string _StatusText = string.Empty;

        /// <summary>
        /// Gets the StatusText property.
        /// TODO Update documentation:
        /// Changes to that property's value raise the PropertyChanged event. 
        /// This property's value is broadcasted by the Messenger's default instance when it changes.
        /// </summary>
        public string StatusText
        {
            get
            {
                return _StatusText;
            }

            set
            {
                if (_StatusText == value)
                {
                    return;
                }

                var oldValue = _StatusText;
                _StatusText = value;

                // Update bindings, no broadcast
                RaisePropertyChanged(StatusTextPropertyName);

            }
        }

        void ChangeStatusText()
        {
            if (Status)
            {
                this.StatusText = "关闭";
            }
            else
            {
                this.StatusText = "打开";
            }
        }

        public ICommand DoCommand
        {
            get
            {
                return new RelayCommand(() =>
                    {
                        if (Status == true)
                        {
                            RC.Current.SerialPortService.ClosePort();

                        }
                        else
                        {
                            RC.Current.SerialPortService.OpenPort();

                        }
                        Status = !Status;
                    });
            }
        }

    }
}